import { create, update, remove, query,deleteData } from '../../../../../framework/service/index';
import { ToastTips } from '../../../../../common/toastTips/tosat';
import { routerRedux } from 'dva/router';

export default {

  namespace: 'setEducation',

  state: {
    id: 0,
    entranceTime:'', //入学时间
    graduationTime:'', //毕业时间
    collegeName:'', //院校名称
    education:'', //学历
    major:'', //专业
    description:'', //在校情况
    pageStatus:'add',
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/set-education') {
          // dispatch({
          //   type: 'query',
          // })
        }
      })
    },
  },



  effects: {
    *query({ payload }, { call, put }) {

    },

    *addMyEducation({ payload }, { call, put, select }) {

      const { entranceTime, graduationTime, collegeName,
        education, major, description } = yield select (({setEducation}) => setEducation);

      const dataInfo = {
        entranceTime,
        graduationTime,
        collegeName,
        education,
        major,
        description
      }

      // console.log('dataInfo', JSON.stringify(dataInfo));
      // return;

      const { code, data, message } = yield call(create, `/api/applicant/eduBackground`, {...dataInfo});

      if(code == 200){
        ToastTips('保存成功', 2000, null);
        yield put(routerRedux.goBack());
      }else{
        ToastTips('保存失败', 2000, null);
      }

    },

    *putWorkTime({ payload }, { call, put, select }) {

      const {id, entranceTime, graduationTime, collegeName,
       education, major, description} = yield select (({setEducation}) => setEducation);

      const dataInfo = {
        id,
        entranceTime,
        graduationTime,
        collegeName,
        education,
        major,
        description
      }

      // console.log('dataInfo', JSON.stringify(dataInfo));
      // return;
      const { code, data, message } = yield call(update, `/api/applicant/eduBackground/${id}`, {...dataInfo});
      if(code == 200){
        ToastTips('修改成功', 2000, null);
        yield put(routerRedux.goBack());
      }else{
        ToastTips('修改失败', 2000, null);
      }
    },

    *deleteEducation({ payload }, { call, put, select }) {
      const { id } = payload;
      const { code, data, message } = yield call(deleteData, `/api/applicant/eduBackground/${id}`, {});
      if(code == 200){
        ToastTips('删除成功', 2000, null);
        yield put(routerRedux.goBack());
      }else{
        ToastTips('删除失败', 2000, null);
      }
    },
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
